package com.imjohn.controller;

import com.imjohn.util.jwtUtil;
import com.imjohn.util.respUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api")
public class CommonController {
    /*生产token*/
    @RequestMapping("/getToken")
    public Map getToken( HttpServletRequest request, HttpServletResponse response) {
        /*session操作*/
        HttpSession session = request.getSession();
        String userName = session.getAttribute("userName").toString();
        /*获取token*/
        String token = jwtUtil.createToken(userName, request);
        /* cookie设置token*/
        Cookie cookie = new Cookie("token", token);
        cookie.setPath("/");
        cookie.setMaxAge(60*30);
        response.addCookie(cookie);
        Map res = new HashMap();
        res.put("token", token);
        return respUtil.respSuccessHash(res);
    }
}
